home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 810 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.9 KB  |  76 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Referencing pointers after delete
  5. Date: 21 Mar 1996 12:01:42 PST
  6. Organization: Fakultdt f|r Mathematik und Informatik
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4isca2$t20@news.BelWue.DE>
  9. References: <4is05t$ceo@engnews1.Eng.Sun.COM>
  10. Reply-To: dietmar.kuehl@uni-konstanz.de
  11. NNTP-Posting-Host: isolde.mti.sgi.com
  12. X-Original-Date: 21 Mar 1996 19:56:50 GMT
  13. X-Newsreader: TIN [version 1.2 PL2]
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBVAwUBMVG1u0y4NqrwXLNJAQFR1QH/Wrk/dKwn2eGO5XYKKTYXnsO/tRqiD+pi
  16.     n0PvG5nqAb5ZjGeZqEE239qEu1x7ONLyvSB+pOOrfmK/0HbUM1dYmA==
  17.     =/Sxo
  18. Originator: austern@isolde.mti.sgi.com
  19.  
  20. Hi,
  21.  
  22. joe (j.) halpin (jhalpin@bnr.ca) wrote:
  23. : In 3.7.3.2.4 the January working paper says:
  24.  
  25. : 4 A deallocation function can free the storage referenced by the pointer
  26. :   given  as  its  argument and renders the pointer invalid.  The storage
  27. :   can be made available for further allocation.  An invalid pointer con-
  28. :   tains an unusable value:  it cannot even be used in an expression.
  29.  
  30. : This sounds as though, in the following:
  31.  
  32. : char *pc = new char[128];
  33. : delete pc;
  34. : pc = 0;
  35.  
  36. : it makes the final assignment (an expression) invalid.
  37.  
  38. No. You cannot use the value of the pointer but you can use the pointer
  39. (actually, you get undefined behavior in the code above because you
  40. delete an array object with 'delete' instead of 'delete[]'; I assume
  41. you took the correct form). Code like this is non-portable:
  42.  
  43.   char *start = strcpy(new char[10], "foobar");
  44.   char *found = strchr(start, 'o');
  45.   delete[] start;
  46.   if (found == start)
  47.     ...
  48.  
  49. : Am I misunderstanding something, or is it illegal to zero out pointers
  50. : after they've been deallocated? I'm assuming that the intent was to
  51. : disallow dereferencing of pointers that have been handed to
  52. : delete. The wording seems to disallow the above as well.
  53.  
  54. The wording is a little bit stronger than just disallowing
  55. dereferencing: There is no portable use of the value of the pointer
  56. after deleting the object. I.e. you cannot use the value to calculate
  57. an pointer to another object and you cannot use the value to compare it
  58. with another pointer.
  59.  
  60. : In fact, it sounds like it also rules out things like 'if(pc == 0)
  61. : ...' after the above fragment.
  62.  
  63. No, it doesn't: If you apply 'delete' to a NULL pointer, nothing
  64. happens.
  65. --
  66. dietmar.kuehl@uni-konstanz.de
  67. http://www.informatik.uni-konstanz.de/~kuehl
  68. I am a realistic optimist - that's why I appear to be slightly pessimistic
  69. ---
  70. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  71.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  72.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  73.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  74.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  75. ]
  76.